package com.medical.project.system.controller;

import java.util.List;

import com.medical.common.utils.StringUtils;
import com.medical.project.system.service.IFamilyMemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.medical.framework.aspectj.lang.annotation.Log;
import com.medical.framework.aspectj.lang.enums.BusinessType;
import com.medical.project.system.domain.JoInRegister;
import com.medical.project.system.service.IJoInRegisterService;
import com.medical.framework.web.controller.BaseController;
import com.medical.framework.web.domain.AjaxResult;
import com.medical.common.utils.poi.ExcelUtil;
import com.medical.framework.web.page.TableDataInfo;

/**
 * 参合缴费登记Controller
 *
 * @author tale
 * @date 2020-06-16
 */
@RestController
@RequestMapping("/system/pay")
public class JoInRegisterController extends BaseController {
    @Autowired
    private IJoInRegisterService joInRegisterService;

    @Autowired
    private IFamilyMemberService familyMemberService;

    /**
     * 查询参合缴费登记列表
     */
    @PreAuthorize("@ss.hasPermi('system:pay:list')")
    @GetMapping("/list")
    public TableDataInfo list(JoInRegister joInRegister) {
        startPage();
        List<JoInRegister> list = joInRegisterService.selectJoInRegisterList(joInRegister);
        return getDataTable(list);
    }

    /**
     * 查询是否参合
     *
     * @param joinIdCard
     * @return
     */
    @GetMapping("/getOne")
    public AjaxResult getOne(String joinIdCard) {
        System.out.println("joinIdCard" + joinIdCard);

        if (StringUtils.isEmpty(joinIdCard)) {

            return AjaxResult.error("请输入身份证号");
        }
        JoInRegister one = joInRegisterService.getOne(joinIdCard);

        if (ObjectUtils.isEmpty(one)){
            return AjaxResult.error("该身份证号没有参合");
        }

        return AjaxResult.success("one", one);
    }

    /**
     * 导出参合缴费登记列表
     */
    @PreAuthorize("@ss.hasPermi('system:pay:export')")
    @Log(title = "参合缴费登记", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(JoInRegister joInRegister) {
        List<JoInRegister> list = joInRegisterService.selectJoInRegisterList(joInRegister);
        ExcelUtil<JoInRegister> util = new ExcelUtil<JoInRegister>(JoInRegister.class);
        return util.exportExcel(list, "pay");
    }

    /**
     * 获取参合缴费登记详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:pay:query')")
    @GetMapping(value = "/{joinNhCode}")
    public AjaxResult getInfo(@PathVariable("joinNhCode") String joinNhCode) {
        return AjaxResult.success(joInRegisterService.selectJoInRegisterById(joinNhCode));
    }

    /**
     * 新增参合缴费登记
     */
    @PreAuthorize("@ss.hasPermi('system:pay:add')")
    @Log(title = "参合缴费登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JoInRegister joInRegister) {
        String familyId = joInRegister.getFamilyId();

        String suffix  = System.currentTimeMillis() + "";
        suffix = suffix.substring(suffix.length() / 2);
        String joinNhCode = familyId + suffix;

        joInRegister.setJoinNhCode(joinNhCode);

        int flag = familyMemberService.updateState(joInRegister.getJoinIdCard());
        if (flag < 0){
            return AjaxResult.error();
        }

        return toAjax(joInRegisterService.insertJoInRegister(joInRegister));
    }

    /**
     * 修改参合缴费登记
     */
    @PreAuthorize("@ss.hasPermi('system:pay:edit')")
    @Log(title = "参合缴费登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JoInRegister joInRegister) {
        return toAjax(joInRegisterService.updateJoInRegister(joInRegister));
    }

    /**
     * 删除参合缴费登记
     */
    @PreAuthorize("@ss.hasPermi('system:pay:remove')")
    @Log(title = "参合缴费登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{joinNhCodes}")
    public AjaxResult remove(@PathVariable String[] joinNhCodes) {
        return toAjax(joInRegisterService.deleteJoInRegisterByIds(joinNhCodes));
    }
}
